#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/4/7 14:56
# @File    : 布局.py
# @Description : -i http://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn


import tkinter as tk
from datetime import datetime


class HoverButton(tk.Button):
    def __init__(self, master, **kw):
        super().__init__(master, **kw)
        self.hover_start_time = None
        self.bind("<Enter>", self.on_enter)
        self.bind("<Leave>", self.on_leave)

    def on_enter(self, event):
        # 当鼠标进入按钮时记录时间
        self.hover_start_time = datetime.now()
        print("Mouse entered at:", self.hover_start_time)

    def on_leave(self, event):
        # 当鼠标离开按钮时计算并打印悬停时间
        if self.hover_start_time:
            hover_end_time = datetime.now()
            hover_duration = hover_end_time - self.hover_start_time
            print("Mouse left after:", hover_duration)
            self.hover_start_time = None  # 重置悬停开始时间


root = tk.Tk()

hover_button = HoverButton(root, text="Hover over me")
hover_button.pack(pady=20, padx=20)

root.mainloop()